package practice1_100;

import java.util.Arrays;

public class Practice1011 {
    public int shipWithinDays(int[] weights, int D) {
        //左边界，找到数组中最大值，右边界为数组的总和。
        int left= Arrays.stream(weights).max().getAsInt(),right=Arrays.stream(weights).sum();
        while (left<right){
            int mid=(left+right)/2;
            int tempWeighrt=0;
            int numOfTime=0;
            for (int i = 0; i < weights.length; i++) {
                if(weights[i]+tempWeighrt>left){
                    tempWeighrt=0;
                    numOfTime++;
                }
                tempWeighrt+=weights[i];
            }
            if (numOfTime <= D) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
//    public  int as(int a,String b){
//        return 1;
//    }
//    public  String as(int a,String b){
//        return "";
//    }
}
